package controllers

import "blog-admin/models"

type ArticleController struct {
	CommonController
}

func (this *ArticleController) List() {
	this.Data["IsCategory"] = true
	articles, _ := models.GetArticlesList()
	this.Data["Article"] = articles
	this.TplName = "article_list.html"
}

func (this *ArticleController) Detail() {
	id := this.Ctx.Input.Param(":id")
	article, _ := models.GetArticleDetail(id)
	//fmt.Println(article)
	this.Data["Article"] = article
	this.TplName = "article_add.html"
}
func (this *ArticleController) Edit() {
	id := this.Ctx.Input.Param(":id")
	article, _ := models.GetArticleDetail(id)
	this.Data["Article"] = article
	this.TplName = "article_add.html"
}
func (this *ArticleController) Delete() {
	id := this.Ctx.Input.Param(":id")
	row := models.DeleteArticle(id)
	if row > 0 {
		this.Success("删除成功", "/article/list", 2)
	} else {
		this.Error("删除失败", "/article/list", 2)
	}
}
func (this *ArticleController) Save() {
	var article = make(map[string]interface{})
	article["id"] = this.GetString("id")
	article["title"] = this.GetString("title")
	article["content"] = this.GetString("content")
	article["is_show"] = this.GetString("is_show")
	article["summary"] = this.GetString("summary")
	article["source"] = this.GetString("source")
	row := models.Save(article)
	if row > 0 {
		this.Success("操作成功", "/article/list", 2)
	} else {
		this.Error("没有修改或操作失败", "/article/list", 2)
	}
}
func (this *ArticleController) GetList() {
	this.Data["IsCategory"] = true
	articles, _ := models.GetArticlesList()
	this.Data["json"] = articles
	this.ServeJSON()
}
func (this *ArticleController) GetDetail() {
	id := this.Ctx.Input.Param(":id")
	article, _ := models.GetArticleDetail(id)
	//fmt.Println(article)
	this.Data["json"] = &article
	this.ServeJSON()
}
